﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class Mp3Album
    {
        private string _AlbumName;
        private string _SingerName;
        private string _ImgLink;
        private List<Mp3Song> _ListSong;
        private string _LocalLink;
        private bool _IsSetInfo;

        public string AlbumName
        {
            get { return _AlbumName; }
            private set { _AlbumName = value; }
        }
        public string SingerName
        {
            get { return _SingerName; }
            private set { _SingerName = value; }
        }
        public string ImgLink
        {
            get { return _ImgLink; }
            private set { _ImgLink = value; }
        }
        public List<Mp3Song> ListSong
        {
            get { return _ListSong; }
            private set { _ListSong = value; }
        }

        public Mp3Album(string localLink, string imgLink)
        {
            _LocalLink = localLink;
            _AlbumName = "";
            _SingerName = "";
            _ImgLink = imgLink;
            _ListSong = new List<Mp3Song>();
            _IsSetInfo = false;
            
        }

        public Mp3Album(string albumName, string singerName, string imgLink, string localLink)
        {
            _AlbumName = albumName;
            _SingerName = singerName;
            _ImgLink = imgLink;
            _LocalLink = localLink;
            _ListSong = new List<Mp3Song>();
            _IsSetInfo = false;
        }

        public async Task SetAllInformation()
        {
            if (_IsSetInfo)
                return;
            try
            {
                Task<string> taskSource = Mp3Zing.DownloadString(new Uri(_LocalLink));
                string source = await taskSource;
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34)
                    {
                        source = source.Remove(i, 1);
                        i--;
                    }
                    i++;
                }

                if (_AlbumName == "")
                    SetAlbumName(source);
                if (_SingerName == "")
                    SetSingerName(source);
                if (_ListSong.Count == 0)
                    await SetListSong(source);
                _IsSetInfo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        private void SetAlbumName(string source)
        {
            string pattern = @"(?<=<h1>).*?(?=</h1>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _AlbumName = mc[0].Value;
        }

        private void SetSingerName(string source)
        {
            string pattern = @"(?<=<h2>).*?(?=</h2>)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            _SingerName = mc[0].Value;
        }

        private async Task SetListSong(string source)
        {
            try
            {
                List<string> listSingerName = new List<string>();
                List<string> listSongName = new List<string>();
                List<string> listLinkSong = new List<string>();

                // lay ten bai hat
                string pattern = @"(?<=<h3>).*?(?=</h3>)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                foreach (Match m1 in mc)
                {
                    listSongName.Add(m1.Value);
                }

                // lay ten ca si
                pattern = @"(?<=<h4>).*?(?=</h4>)";
                Regex regex0 = new Regex(pattern);
                MatchCollection mc0 = regex.Matches(source);
                foreach (Match m1 in mc0)
                {
                    listSingerName.Add(m1.Value);
                }

                // lay ds link 
                pattern = @"(?<=xml=).*?(?=class)";
                Regex regex1 = new Regex(pattern);
                MatchCollection mc1 = regex.Matches(source);
                string tempSource = mc[0].Value;

                int i = 0;
                while (i < tempSource.Length)
                {
                    if (tempSource[i] == 34 || tempSource[i] == 32)
                    {
                        tempSource = tempSource.Remove(i, 1);
                        i--;
                    }
                    i++;
                }
                tempSource = await Mp3Zing.DownloadString(new Uri(tempSource));
                pattern = @"(?=http).*?(?==)";
                Regex regex2 = new Regex(pattern);
                MatchCollection mc2 = regex.Matches(tempSource);


                int j = 0;
                foreach (Match m1 in mc2)
                {
                    string tempLink = m1.Value;

                    i = 0;
                    while (i < tempLink.Length)
                    {
                        if (tempLink[i] == 92 || tempLink[i] == 61)
                        {
                            tempLink = tempLink.Remove(i, 1);
                            i--;
                        }
                        i++;
                    }

                    listLinkSong.Add(tempLink);
                }

                // cap nhat danh sach bai hat trong album
                j = 0;
                foreach (string s in listLinkSong)
                {
                    _ListSong.Add(new Mp3Song(listSongName[j], listSingerName[j], s, false));
                    j++;
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

    }
   
}
